﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudApiServer.Services
{
    /// <summary>
    /// 就诊记录查询服务
    /// </summary>
    public class FsiIntegratedQueryService
    {

        /// <summary>
        /// 5301人员慢特病查询
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="ControllerName">来源控制器名称</param>
        /// <returns>出参</returns>
        /// <exception cref="Exception"></exception>
        public async Task<OutputQueryPsnOpspReg> QueryPsnOpspReg(FsiRequestHeader<InputQueryPsnOpspReg> fsiRequest, string ControllerName)
        {
            try
            {
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                string psn_no = fsiRequest.Input.data.psn_no;
                if (string.IsNullOrEmpty(psn_no))
                {
                    throw new Exception("参数校验失败：传入的人员编号psn_no为空，请检查入参！");
                }
                string connectionString = $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
                string query="select * from fsi_querypsnopspreg where psn_no=@psn_no";
                var parameter = new List<MySqlParameter>
                {
                    new MySqlParameter("@psn_no", psn_no),
                };
                DataTable dt = await MySqlHelper.ExecuteQueryAsync(connectionString, query, parameter);
                List<FeedetailQueryPsnOpspReg> dataList = new List<FeedetailQueryPsnOpspReg>();
                foreach (DataRow row in dt.Rows)
                {
                    FeedetailQueryPsnOpspReg item = new FeedetailQueryPsnOpspReg
                    {
                        opsp_dise_code = row["opsp_dise_code"].ToString(),
                        opsp_dise_name = row["opsp_dise_name"].ToString(),
                        exp_content = row["exp_content"].ToString(),
                        begndate = row["begndate"].ToString(),
                        enddate = row["enddate"].ToString(),

                    };
                    dataList.Add(item);
                }
                var output = new OutputQueryPsnOpspReg
                {
                    feedetail = dataList,
                };
                return output;

            }
            catch (Exception ex)
            {
                LogHelper.Error(
                    $"[FsiIntegratedQueryService.QueryPsnOpspReg]人员慢特病查询异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
                throw;
            }
        }
        
        /// <summary>
        /// 5206年度费用累计
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="ControllerName">来源控制器名称</param>
        /// <returns>出参</returns>
        /// <exception cref="Exception"></exception>
        public async Task<outputCuminfo> QueryFixmedinsPracPsnSum(FsiRequestHeader<InputQueryFixmedinsPracPsnSum> fsiRequest, string ControllerName)
        {
            try
            {
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                string psn_no = fsiRequest.Input.data.psn_no;
                if (string.IsNullOrEmpty(psn_no))
                {
                    throw new Exception("参数校验失败：传入的人员编号psn_no为空，请检查入参！");
                }
                string connectionString = $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
                string query="select * from fsi_queryfixmedinspracpsnsum where psn_no=@psn_no";
                var parameter = new List<MySqlParameter>
                {
                    new MySqlParameter("@psn_no", psn_no),
                };
                DataTable dt = await MySqlHelper.ExecuteQueryAsync(connectionString, query, parameter);
                List<Cuminfo> dataList = new List<Cuminfo>();
                foreach (DataRow row in dt.Rows)
                {
                    Cuminfo item = new Cuminfo
                    {
                        cum_type_code = row["cum_type_code"].ToString(),
                        year = row["year"].ToString(),
                        cum = (decimal)row["cum"],
                        cum_ym = row["cum_ym"].ToString(),
                        insutype = row["insutype"].ToString()
                    };
                    dataList.Add(item);
                }
                var output = new outputCuminfo
                {
                    cuminfo = dataList
                };
                return output;

            }
            catch (Exception ex)
            {
                LogHelper.Error(
                    $"[FsiIntegratedQueryService.QueryFixmedinsPracPsnSum]人员年度费用累计查询异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
                throw;
            }
        }
    }
}
